<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card style="margin-top: 20px">
      <span>
        <el-input
          style="width: 475px"
          placeholder="请输入内容"
          v-model="input4"
        >
          <i slot="suffix" class="el-input__icon el-icon-search"></i>
        </el-input>
      </span>
      <!-- 内容 -->
      <el-table
        stripe
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px"
      >
        <el-table-column width="50" type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="440">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" width="80">
        </el-table-column>
        <el-table-column prop="pay_status" label="是否付款" width="105">
          <!-- 未完成 -->
          <template #default="{ row }">
            <el-tag type="success" v-if="row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="80">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间"> </el-table-column>
        <el-table-column prop="address" label="操作">
          <template #default="{ row }">
            <el-button
              style="width: 60px"
              type="primary"
              icon="el-icon-edit"
              @click="xiugai(row)"
            ></el-button>
            <el-button
              @click="kuaidi(row)"
              style="width: 60px"
              type="success"
              icon="el-icon-location"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="2"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 修改用户弹框 -->
    <el-dialog title="修改订单数据" :visible.sync="detailsUser">
      <el-form :rules="rules" :model="formDate">
        <el-form-item label="订单价格" label-width="120px" prop="order_price">
          <el-input
            autocomplete="off"
            v-model="formDate.order_price"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否付款" label-width="120px">
          <el-radio-group v-model="formDate.pay_status">
            <el-radio :label="'0'">未付款</el-radio>
            <el-radio :label="'1'">已付款</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否发货" label-width="120px">
          <el-radio-group v-model="formDate.is_send">
            <el-radio label="是">是</el-radio>
            <el-radio label="否">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" @click="btn">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 物流弹框 -->
    <el-dialog title="物流进度" :visible.sync="dialogTableVisible">
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          :timestamp="activity.timestamp"
        >
          {{ activity.content }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { getOrders, getModify, getLogistics } from '@/api/orders'
export default {
  name: 'my-orders',
  data () {
    return {
      // 物流
      reverse: true,
      activities: [
        {
          content: '已签收感谢使用顺丰,期待再次为您服务',
          timestamp: '2018-05-10 09:39:00'
        },
        {
          content:
            '[北京市]北京海淀育新小区营业点派件员顺丰速运95338正在为您派件',
          timestamp: '2018-05-10 08:23:00'
        },
        {
          content: '快件到达[北京海淀育新小区营业点]',
          timestamp: '2018-05-1007:32:00'
        },
        {
          content:
            '快件在[北京顺义集散中心]已装车,准备发往[北京海淀育新小区营业点]',
          timestamp: '2018-05-10 02:03:00'
        },
        {
          content: '快件到达[北京顺义集散中心]',
          timestamp: '2018-05-09 23:05:00'
        },
        {
          content: '快件在[北京宝胜营业点]已装车,准备发往[北京顺义集散中心]',
          timestamp: '2018-05-09 21:21:00'
        },
        {
          content: '顺丰速运已收取快件',
          timestamp: '2018-05-0913:07:00'
        },
        {
          content: '卖家发货',
          timestamp: '2018-05-09 12:25:03'
        },
        {
          content: '您的订单将由HLA(北京海淀区清河中街店)门店安排发货',
          timestamp: '2018-05-0912:22:24'
        },
        {
          content: '商品已经下单',
          timestamp: '2018-05-08 21:36:04'
        }
      ],
      // 编辑校验
      rules: {
        order_price: [
          {
            required: true,
            message: '请输入价格'
          }
        ]
      },
      radio: '1',
      detailsUser: false, // 修改弹框显示隐藏
      dialogTableVisible: false, // 物流弹框
      input4: '',
      currentPage4: 1, // 默认第几页
      total: 0, // 总数据几条
      tableData: [], // 首次获取数据渲染列表
      formDate: {}, // 当前行数据
      myUserPage: {
        query: '',
        pagenum: 1, // 当前页
        pagesize: 5 // 每页
      }
    }
  },
  created () {
    // 获取用户列表
    this.myUser()
  },

  methods: {
    // 获取用户列表
    async myUser () {
      const res = await getOrders(this.myUserPage)
      console.log(res)
      console.log(res.data.goods)
      // 共计多少条
      this.total = res.data.total
      this.tableData = res.data.goods
    },
    // 分页
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.myUserPage.pagesize = val
      this.myUser()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      // 点击哪一页给到当前页
      this.myUserPage.pagenum = val
      this.myUser()
    },
    // 回显
    xiugai (value) {
      // 显示弹框
      this.detailsUser = true
      console.log(value)
      this.formDate = value
    },
    // 修改
    async btn () {
      // console.log(this.formDate.order_id)
      const res = await getModify(this.formDate.order_id, this.formDate)
      console.log(res)
      this.$message.success(res.meta.msg)
      // 关闭弹框
      this.detailsUser = false
      // 调用最新数据
      this.myUser()
    },
    // 物流
    async kuaidi (row) {
      console.log(row)
      // 打开弹框
      this.dialogTableVisible = true
      const res = await getLogistics(row.order_id)
      console.log(res)
    }
  }
}
</script>

<style scoped lang="scss">
.block {
  margin-top: 15px;
}
.box-card {
  margin-top: 20px;
}
.el-input__icon {
  width: 50px;
  height: 38px;
  background-color: #f5f7fa;
  border: 1px solid #dcdfe6;
  margin-right: -58px;
  border-radius: 2px;
}
</style>
